package edu.jkd.controller;

import edu.jkd.entity.Myorder;
import edu.jkd.entity.Product;
import edu.jkd.service.IMyorderService;
import edu.jkd.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Controller
public class MyOrderController {

    @Autowired
    private IMyorderService myorderService;
    @Autowired
    private IProductService productService;

    @PostMapping("order")
    public ModelAndView save(Integer proId,Float proPrice){
        Myorder order = new Myorder();
        order.setOrderNumber(UUID.randomUUID().toString().replace("-",""));

        //
        order.setCustId(1);
        order.setProId(proId);
        order.setOrderPrice(proPrice);
        order.setOrderCreateTime(LocalDateTime.now());
        LocalDateTime dateTime=LocalDateTime.now();
        LocalDateTime dateTime1=dateTime.plusDays(1);
        order.setCheckInTime(LocalDateTime.now());
        order.setCheckOutTime(dateTime1);



        order.setStatus(1);
        myorderService.save(order);
        Product product = productService.getById(order.getProId());

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("order",order);
        modelAndView.addObject("product",product);
        modelAndView.setViewName("confirm");
        return modelAndView;

    }


    //获取所有订单
    @GetMapping("order")
    public  ModelAndView getAll(Integer customerId){
        customerId=1;
        List<Myorder> myorderList = myorderService.getByCustomerId(customerId);
        for(Myorder myorder:myorderList){
            String proName  = productService.getById(myorder.getProId()).getProName();
            myorder.setOther1(proName);
        }
        ModelAndView mav = new ModelAndView("order_list");
        mav.addObject("myorderList",myorderList);
        return mav;
    }
}